<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.1.1/jquery.min.js"></script>
  <script src="bootstrap/js/bootstrap.js"></script>
  <link rel="stylesheet" href="bootstrap/css/bootstrap.css">
  <script>
    let currentPage = 1;
    window.onload = function() {
      initData(1,5);
    }
    function initData(page,size) {
      currentPage = page;
      $.ajax({
        url:'allStu',
        type:'GET',
        data:{
          'page':page,
          'size':size
        },
        dataType:'json', // 修改为 json 类型
        success:function(res) {
         if (res.code === 200){
           $('#tb').empty();
           $('#tdbar').empty();
           let data_temp = res.data
           for (let i = 0; i < data_temp.length; i++){
             let birthDateStr = data_temp[i].birthDate ? (data_temp[i].birthDate.year ? data_temp[i].birthDate.year + '-' + (data_temp[i].birthDate.month || '') + '-' + (data_temp[i].birthDate.day || '') : '') : '';
$('<tr><td><input type="checkbox" name="chk" value="'+(data_temp[i].studentId || '')+'"></td><td>'+('')+'</td><td>'+(data_temp[i].studentId || '')+'</td><td>'+(data_temp[i].name || '')+'</td><td>'+(data_temp[i].gender || '')+'</td><td>'+birthDateStr+'</td><td>'+(data_temp[i].idCard || '')+'</td><td>'+(data_temp[i].college || '')+'</td><td>'+(data_temp[i].major || '')+'</td><td>'+(data_temp[i].className || '')+'</td><td>'+(data_temp[i].status === 1 ? '在读' : (data_temp[i].status === 0 ? '毕业' : data_temp[i].status || ''))+'</td><td>'+(data_temp[i].dormId || '')+'</td><td>'+(data_temp[i].bedNumber || '')+'</td><td>'+(data_temp[i].phone || '')+'</td><td>'+(data_temp[i].email || '')+'</td><td>'+(data_temp[i].emergencyContact || '')+'</td><td>'+(data_temp[i].username || '')+'</td><td>'+(data_temp[i].password || '')+'</td><td><button class="btn btn-primary">编辑</button>&nbsp;<button class="btn btn-danger" onclick="delRow('+data_temp[i].studentId+','+ currentPage +')">毕业</button></td></tr>').prependTo('#tb');
           }
           $('<button class="btn btn-primary" onclick="initData(1,'+size+')">首页</button>&nbsp;<button class="btn btn-primary" onclick="initData('+(page > 1 ? page-1 : 1)+','+size+')">上一页</button>&nbsp;<button class="btn btn-primary" onclick="initData('+(page+1)+','+size+')">下一页</button>&nbsp;<button class="btn btn-primary" onclick="initData('+res.totalPage+','+size+')">尾页</button>&nbsp;<select class="form-control" style="display:inline; width:150px;" onchange="initData(currentPage, parseInt(this.value))" id="pageSizeSelect"><option value="5" '+(size==5?'selected':'')+'>5</option><option value="10" '+(size==10?'selected':'')+'>10</option><option value="20" '+(size==20?'selected':'')+'>20</option></select>').appendTo('#tdbar');
         }
        },
        error:function(xhr, status, error) {
          console.error('请求失败:', status, error);
          alert('请求商品列表失败，请检查网络或联系管理员');
        }
      })
    }

    //毕业操作逻辑操作
    function delRow(id ,page) {
      let anw =window.confirm("确认毕业?");
      if (anw){ 
        $.ajax({
          url:'delStudent',
          type:'POST',
          data:{
            id:id,
            page: page
          },
          dataType:'text',
          success:function(res) {
            if (res =='Y'){
              alert('毕业成功');
              initData(page,5);
            }else {
              alert('毕业失败，请检查网络或联系管理员');
            }
          },
          error: function(xhr, status, error) {
            console.error('毕业请求失败:', status, error);
            alert('毕业请求失败，请检查网络或联系管理员');
          }
        });
      }
    }


  </script>
</head>
<body>
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">商品搜索</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">

      <form class="navbar-form navbar-left">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="请输入查询关键字" oninput="queryData(this.value)">

        </div>
<!--        <button type="submit" class="btn btn-default">查询</button>-->
      </form>
      <button class="btn btn-default" style="margin-left: 50px;" onclick="delBatch()">批量毕业</button>

    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
<script>

  //批量毕业
  function delBatch() {
    let anw = window.confirm("确认批量毕业?");
    if (anw) {
      // 重新收集选中的学生ID
      selectedIds = [];
      $('input[name="chk"]:checked').each(function() {
        selectedIds.push($(this).val());
      });
      
      if (selectedIds.length > 0) {
        $.ajax({
          url: 'delAllStu',
          type: 'POST',
          data: {
            ids: selectedIds.join(','),
            page: currentPage
          },
          dataType: 'text',
          success: function(res) {
            if (res === 'Y') {
              alert('批量毕业成功');
              initData(currentPage, 5);
            } else {
              alert('批量毕业失败，请检查网络或联系管理员');
            }
          },
          error: function(xhr, status, error) {
            console.error('批量毕业请求失败:', status, error);
            alert('批量毕业请求失败，请检查网络或联系管理员');
          }
        });
      } else {
        alert('请选择需要毕业的学生');
      }
    }
  }
  let selectedIds = [];

  //多选框全选
  function reSelectAll() {
    let chkval = document.getElementById("checkvalue");
    let chks = document.getElementsByName('chk');
    
    selectedIds = []; // Reset selected IDs array
    
    for (let i = 0; i < chks.length; i++) {
      chks[i].checked = chkval.checked;
      if (chks[i].checked) {
        selectedIds.push(chks[i].value);
      }
    }

  }

  //查询数据
  function  queryData(keywords, page = 1, size = 5) {
    if (keywords === '') {
      initData( 1, size);
      return;
    }
    $.ajax({
      url: 'queryStudent',
      type: 'GET',
      data: {
        'keywords': keywords,
        'page': page,
        'size': size
      },
      dataType: 'json', 
      success: function(res) {
        if (res.code === 200) {
          $('#tb').empty();
          $('#tdbar').empty();
          let data_temp = res.data;
          for (let i = 0; i < data_temp.length; i++) {
            let birthDateStr = data_temp[i].birthDate ? (data_temp[i].birthDate.year ? data_temp[i].birthDate.year + '-' + (data_temp[i].birthDate.month || '') + '-' + (data_temp[i].birthDate.day || '') : '') : '';
            $('<tr><td><input type="checkbox" name="chk" value="'+(data_temp[i].studentId || '')+'"></td><td>'+('')+'</td><td>'+(data_temp[i].studentId || '')+'</td><td>'+(data_temp[i].name || '')+'</td><td>'+(data_temp[i].gender || '')+'</td><td>'+birthDateStr+'</td><td>'+(data_temp[i].idCard || '')+'</td><td>'+(data_temp[i].college || '')+'</td><td>'+(data_temp[i].major || '')+'</td><td>'+(data_temp[i].className || '')+'</td><td>'+(data_temp[i].status === 1 ? '在读' : (data_temp[i].status === 0 ? '毕业' : data_temp[i].status || ''))+'</td><td>'+(data_temp[i].dormId || '')+'</td><td>'+(data_temp[i].bedNumber || '')+'</td><td>'+(data_temp[i].phone || '')+'</td><td>'+(data_temp[i].email || '')+'</td><td>'+(data_temp[i].emergencyContact || '')+'</td><td>'+(data_temp[i].username || '')+'</td><td>'+(data_temp[i].password || '')+'</td><td><button class="btn btn-primary">编辑</button>&nbsp;<button class="btn btn-danger" onclick="delRow('+data_temp[i].studentId+','+ page +')">毕业</button></td></tr>').prependTo('#tb');
          }
          $('<button class="btn btn-primary" onclick="queryData(\'' + keywords + '\', 1, ' + size + ')">首页</button>&nbsp;<button class="btn btn-primary" onclick="queryData(\'' + keywords + '\', ' + (page > 1 ? page - 1 : 1) + ', ' + size + ')">上一页</button>&nbsp;<button class="btn btn-primary" onclick="queryData(\'' + keywords + '\', ' + (page + 1) + ', ' + size + ')">下一页</button>&nbsp;<button class="btn btn-primary" onclick="queryData(\'' + keywords + '\', ' + res.totalPage + ', ' + size + ')">尾页</button>&nbsp;<select class="form-control" style="display:inline; width:150px;" onchange="queryData(\'' + keywords + '\', page, parseInt(this.value))" id="pageSizeSelect"><option value="5" '+(size==5?'selected':'')+'>5</option><option value="10" '+(size==10?'selected':'')+'>10</option><option value="20" '+(size==20?'selected':'')+'>20</option></select>').appendTo('#tdbar');
        }
      },
      error: function(xhr, status, error) {
        console.error('查询请求失败:', status, error);
        alert('查询请求失败，请检查网络或联系管理员');
      }
    });
  }
</script>



<table class="table table-bordered table-striped table-hover" >
  <thead>


  <th>
    <input type="checkbox" value="0"  id= "checkvalue" onclick="reSelectAll()"></input>
  </th>
  <th>#</th>
  <th>学号</th>
  <th>姓名</th>
  <th>性别</th>
  <th>出生日期</th>
  <th>身份证号</th>
  <th>学院</th>
  <th>专业</th>
  <th>班级</th>
  <th>状态</th>
  <th>宿舍ID</th>
  <th>床位号</th>
  <th>电话</th>
  <th>邮箱</th>
  <th>紧急联系人</th>
  <th>用户名</th>
  <th>密码</th>
  <th>操作</th>

  </thead>
  <tbody id="tb">




  </tbody>
  <tfoot>
  <td colspan="11" style="text-align: center;" id="tdbar">
  </tfoot>
</table>

</body>
</html>